<template>
  <div>
    <Nav :title="$route.name"></Nav>
    <div class="container">
      <van-uploader :after-read="afterRead">
        <div
          class="item_head item_btn item_one"
          @click="$router.push('/permes')"
        >
          <span>头像</span>
          <img :src="url" alt="" />

          <i></i>
        </div>
      </van-uploader>
      <div class="item_head item_btn" @click="$router.push('/NickName')">
        <span>昵称</span>
        <p>{{ nick ? nick : "请设置昵称" }}</p>
        <i></i>
      </div>
      <div class="item_head item_btn m20" @click="show = true">
        <span>性别</span>
        <p>{{ gender ? gender :'设置性别' }}</p>
        <i></i>
      </div>
      <div class="item_head item_btn" @click="$router.push('/addresssMange')">
        <span>股份身份认证</span>
        <p>去认证</p>
        <i></i>
      </div>
      <div class="item_head item_btn">
        <span>苏宁国际实名认证</span>
        <p>去实名</p>
        <i></i>
      </div>
      <button class="exit_login">删除个人信息</button>
    </div>
    <van-popup
      v-model="show"
      round
      position="bottom"
      :style="{ height: '30%' }"
    >
      <p class="select_sex" @click="selectBox(1)">男</p>
      <p class="select_sex" @click="selectGril(2)">女</p>
      <div class="cancel select_sex" @click="cancel">取消</div>
    </van-popup>
  </div>
</template>

<script>
import { fectUserMessage, fetchUpload, userDetail } from '_api'
import { getToken } from '_utils/token.js'
import Nav from '../../components/Nav.vue'

export default {
  components: { Nav },
  data () {
    return {
      url: '',
      nick: '',
      show: false,
      gender: ''
    }
  },
  created () {
    this.userDetail()
  },
  methods: {
    userDetail () {
      userDetail({
        token: getToken()
      }).then(res => {
        console.log(res)
        if (res.data.code === 0) {
          this.nick = res.data.data.base.nick
          this.url = res.data.data.base.avatarUrl
          this.gender = res.data.data.base.gender === 1 ? '男' : '女'
        }
      })
    },
    afterRead (file) {
      // console.log(file.content)
      // this.url = file.content

      var formData = new FormData()
      formData.append('upfile', file.file)
      formData.append('token', getToken())
      this.uploadRe.post('/dfs/upload/file', formData).then(res => {
        if (res.data.code === 0) {
          console.log(res)
          this.url = res.data.data.url
          this.fectUserMessage()
        }
      })
      // fetchUpload({
      //   formData,
      //   token: getToken()
      // }).then(res => {
      //   console.log(res)
      // })
    },
    fectUserMessage () {
      fectUserMessage({
        avatarUrl: this.url,
        token: getToken()
      }).then(res => {
        console.log(res)
      })
    },
    cancel () {
      this.show = false
    },
    select (select) {
      fectUserMessage({
        gender: select,
        token: getToken()
      }).then(res => {
        if (res.data.code === 0) {
          this.show = false
          this.userDetail()
        }
      })
    },
    // 选择 男女
    selectBox (select) {
      this.select(select)
    },
    selectGril (select) {
      this.select(select)
    }

  },
  // 监听 头像和昵称的变化
  watch: {
    url (newVal) {
      this.$store.commit('user/set_info', {
        url: this.url,
        nick: this.nick
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.m20 {
  margin-bottom: 0.266667rem;
}
.container {
  padding-top: 1.2rem;
  .item_head.item_one {
    box-sizing: border-box;
    width: 10rem;
    height: 1.866667rem;
    span {
      font-size: 0.373333rem;
    }
  }
  .item_btn {
    padding: 0 0.333333rem;
    display: flex;
    height: 1.2rem;
    justify-content: space-between;
    align-items: center;
    background-color: #fff;
    border-bottom: 1px solid #f2f2f2;
    img {
      width: 1.2rem;
      height: 1.2rem;
      border-radius: 0.6rem;
      margin-right: 0.266667rem;
    }
    span {
      flex: 1;
      font-size: 0.373333rem;
      color: #333;
    }
    .code_icon {
      width: 0.426667rem;
      height: 0.426667rem;
      background: url("../../assets/vip_code.png") no-repeat;
      background-size: contain;
    }
    p {
      font-size: 0.4rem;
      color: #999;
    }
    i {
      width: 0.16rem;
      height: 0.293333rem;
      background: url("../../assets/ro.png") no-repeat;
      background-size: contain;
      margin-left: 0.266667rem;
    }
  }
}
.exit_login {
  display: block;
  width: 9.066667rem;
  height: 1.066667rem;
  border: none;
  margin: 0.4rem auto 0.533333rem;
  background-color: #ffff;
  line-height: 1.066667rem;
  text-align: center;
  font-size: 0.4rem;
  color: #222;
  border-radius: 0.266667rem;
}
.select_sex:nth-child(1) {
  margin-top: 0.266667rem;
}
.select_sex {
  font-size: 0.4rem;
  height: 1.173333rem;
  text-align: center;
  line-height: 1.173333rem;
  border-bottom: 1px solid #ddd;
}
.cancel {
  border-bottom: 0;
  color: #ffaa00;
}
</style>
